package com.easemob.chat;

import com.easemob.EMCallBack;
import com.easemob.analytics.EMPerformanceCollector;
import com.easemob.analytics.EMTimeTag;
import com.easemob.chat.core.e;
import com.easemob.util.EMLog;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

class EMConversationManager
{
  private static final String TAG = EMConversationManager.class.getSimpleName();
  private static final int DEFAULT_LOAD_MESSAGE_COUNT = 20;
  private static EMConversationManager instance = new EMConversationManager();
  private Hashtable<String, EMMessage> allMessages = new Hashtable();
  private Hashtable<String, EMConversation> conversations = new Hashtable();
  private Hashtable<String, EMConversation> tempConversations = new Hashtable();
  private boolean allConversationsLoaded = false;

  public static EMConversationManager getInstance()
  {
    return instance;
  }

  void loadAllConversations()
  {
    loadAllConversations(20);
  }

  synchronized void loadAllConversations(int paramInt)
  {
    if (this.allConversationsLoaded)
      return;
    this.conversations.clear();
    this.tempConversations.clear();
    EMTimeTag localEMTimeTag = new EMTimeTag();
    localEMTimeTag.start();
    EMLog.d(TAG, "start to load converstations:");
    if (paramInt == 1)
      this.conversations = e.a().c();
    else
      this.conversations = e.a().a(paramInt);
    Iterator localIterator1;
    EMConversation localEMConversation;
    synchronized (this.conversations)
    {
      localIterator1 = this.conversations.values().iterator();
      while (localIterator1.hasNext())
      {
        localEMConversation = (EMConversation)localIterator1.next();
        EMLog.d(TAG, "loaded user " + localEMConversation.getUserName());
      }
    }
    synchronized (this.conversations)
    {
      localIterator1 = this.conversations.values().iterator();
      while (localIterator1.hasNext())
      {
        localEMConversation = (EMConversation)localIterator1.next();
        Iterator localIterator2 = localEMConversation.messages.iterator();
        while (localIterator2.hasNext())
        {
          EMMessage localEMMessage = (EMMessage)localIterator2.next();
          synchronized (this.allMessages)
          {
            this.allMessages.put(localEMMessage.msgId, localEMMessage);
          }
        }
      }
    }
    if ((this.conversations != null) && (this.allMessages != null) && (this.conversations.size() > 0))
      EMPerformanceCollector.collectLoadingAllConversations(this.conversations.size(), this.allMessages.size(), localEMTimeTag.stop());
    this.allConversationsLoaded = true;
  }

  public void asyncloadAllConversations(final EMCallBack paramEMCallBack, final int paramInt)
  {
    new Thread()
    {
      public void run()
      {
        EMConversationManager.this.loadAllConversations(paramInt);
        if (paramEMCallBack != null)
          paramEMCallBack.onSuccess();
      }
    }
    .start();
  }

  public void asyncloadAllConversations(EMCallBack paramEMCallBack)
  {
    asyncloadAllConversations(paramEMCallBack, 20);
  }

  public EMConversation getConversation(String paramString)
  {
    EMLog.d(TAG, "get conversation for user:" + paramString);
    EMConversation localEMConversation = (EMConversation)this.conversations.get(paramString);
    if (localEMConversation != null)
      return localEMConversation;
    localEMConversation = (EMConversation)this.tempConversations.get(paramString);
    if (localEMConversation != null)
      return localEMConversation;
    List localList = null;
    EMGroup localEMGroup = EMGroupManager.getInstance().getGroup(paramString);
    long l;
    if (localEMGroup == null)
    {
      localList = e.a().b(paramString, null, 20);
      l = e.a().a(paramString, false);
      localEMConversation = new EMConversation(paramString, localList, false, Long.valueOf(l));
    }
    else
    {
      localList = e.a().a(paramString, null, 20);
      l = e.a().a(paramString, true);
      localEMConversation = new EMConversation(paramString, localList, true, Long.valueOf(l));
    }
    this.tempConversations.put(paramString, localEMConversation);
    return localEMConversation;
  }

  public EMConversation getConversation(String paramString, boolean paramBoolean)
  {
    EMLog.d(TAG, "get conversation for user:" + paramString);
    EMConversation localEMConversation = (EMConversation)this.conversations.get(paramString);
    if (localEMConversation != null)
      return localEMConversation;
    localEMConversation = (EMConversation)this.tempConversations.get(paramString);
    if (localEMConversation != null)
      return localEMConversation;
    List localList = null;
    long l = 0L;
    if (!paramBoolean)
    {
      localList = e.a().b(paramString, null, 20);
      l = e.a().a(paramString, false);
    }
    else
    {
      localList = e.a().a(paramString, null, 20);
      l = e.a().a(paramString, true);
    }
    localEMConversation = new EMConversation(paramString, localList, paramBoolean, Long.valueOf(l));
    this.tempConversations.put(paramString, localEMConversation);
    return localEMConversation;
  }

  public boolean deleteConversation(String paramString)
  {
    EMLog.d(TAG, "remove conversation for user: " + paramString);
    EMConversation localEMConversation = (EMConversation)this.conversations.get(paramString);
    if (localEMConversation == null)
      localEMConversation = getConversation(paramString);
    if (localEMConversation == null)
      return false;
    return deleteConversation(paramString, localEMConversation.isGroup());
  }

  public boolean deleteConversation(String paramString, boolean paramBoolean)
  {
    return deleteConversation(paramString, paramBoolean, true);
  }

  public boolean deleteConversation(String paramString, boolean paramBoolean1, boolean paramBoolean2)
  {
    EMLog.d(TAG, "remove conversation for user: " + paramString);
    EMConversation localEMConversation = (EMConversation)this.conversations.get(paramString);
    if (localEMConversation == null)
      localEMConversation = getConversation(paramString);
    if (localEMConversation == null)
      return false;
    if (paramBoolean2)
      if (paramBoolean1)
        e.a().i(paramString);
      else
        e.a().f(paramString);
    e.a().d(paramString, paramBoolean1);
    localEMConversation.clear();
    this.conversations.remove(paramString);
    if (this.tempConversations.containsKey(paramString))
      this.tempConversations.remove(paramString);
    return true;
  }

  void deleteAllConversations()
  {
    synchronized (this.conversations)
    {
      Enumeration localEnumeration = this.conversations.keys();
      while (localEnumeration.hasMoreElements())
      {
        String str = (String)localEnumeration.nextElement();
        deleteConversation(str);
      }
    }
  }

  void resetAllUnreadMsgCount()
  {
    synchronized (this.conversations)
    {
      Enumeration localEnumeration = this.conversations.keys();
      while (localEnumeration.hasMoreElements())
      {
        String str = (String)localEnumeration.nextElement();
        ((EMConversation)this.conversations.get(str)).resetUnreadMsgCount();
      }
    }
  }

  public boolean clearConversation(String paramString)
  {
    EMLog.d(TAG, "clear conversation for user: " + paramString);
    EMConversation localEMConversation = (EMConversation)this.conversations.get(paramString);
    if (localEMConversation == null)
      localEMConversation = getConversation(paramString);
    if (localEMConversation == null)
      return false;
    if (localEMConversation.isGroup())
      e.a().i(paramString);
    else
      e.a().f(paramString);
    localEMConversation.clear();
    return true;
  }

  public Hashtable<String, EMConversation> getAllConversations()
  {
    return this.conversations;
  }

  public EMMessage getMessage(String paramString)
  {
    return (EMMessage)this.allMessages.get(paramString);
  }

  void replaceMessageId(String paramString1, String paramString2)
  {
    EMMessage localEMMessage = (EMMessage)this.allMessages.get(paramString1);
    if (localEMMessage != null)
    {
      e.a().a(paramString1, paramString2);
      this.allMessages.remove(paramString1);
      localEMMessage.msgId = paramString2;
      this.allMessages.put(paramString2, localEMMessage);
    }
  }

  boolean areAllConversationsLoaded()
  {
    return this.allConversationsLoaded;
  }

  void addMessage(EMMessage paramEMMessage)
  {
    String str1 = paramEMMessage.msgId;
    if (!this.allMessages.containsKey(str1))
    {
      this.allMessages.put(str1, paramEMMessage);
      String str2;
      EMConversation localEMConversation;
      if (paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat)
      {
        str2 = paramEMMessage.getTo();
        localEMConversation = getConversation(str2, true);
        localEMConversation.addMessage(paramEMMessage);
        if (!this.conversations.containsKey(str2))
          this.conversations.put(str2, localEMConversation);
      }
      else
      {
        if (paramEMMessage.direct == EMMessage.Direct.RECEIVE)
          str2 = paramEMMessage.from.username;
        else
          str2 = paramEMMessage.to.username;
        localEMConversation = getConversation(str2, false);
        localEMConversation.addMessage(paramEMMessage);
        if (!this.conversations.containsKey(str2))
          this.conversations.put(str2, localEMConversation);
      }
    }
  }

  void addMessage(EMMessage paramEMMessage, boolean paramBoolean)
  {
    String str1 = paramEMMessage.msgId;
    if (!this.allMessages.containsKey(str1))
    {
      this.allMessages.put(str1, paramEMMessage);
      String str2;
      EMConversation localEMConversation;
      if (paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat)
      {
        str2 = paramEMMessage.getTo();
        localEMConversation = getConversation(str2, true);
        localEMConversation.addMessage(paramEMMessage, paramBoolean);
        if (!this.conversations.containsKey(str2))
          this.conversations.put(str2, localEMConversation);
      }
      else
      {
        if (paramEMMessage.direct == EMMessage.Direct.RECEIVE)
          str2 = paramEMMessage.from.username;
        else
          str2 = paramEMMessage.to.username;
        localEMConversation = getConversation(str2, false);
        localEMConversation.addMessage(paramEMMessage, paramBoolean);
        if (!this.conversations.containsKey(str2))
          this.conversations.put(str2, localEMConversation);
      }
    }
  }

  private void addConversationToDB(EMMessage paramEMMessage)
  {
    String str = "";
    boolean bool = paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat;
    if (bool)
      str = paramEMMessage.getTo();
    else if (paramEMMessage.getFrom().equals(EMChatManager.getInstance().getCurrentUser()))
      str = paramEMMessage.getTo();
    else
      str = paramEMMessage.getFrom();
    e.a().b(str, bool);
  }

  public void saveMessage(EMMessage paramEMMessage)
  {
    EMLog.d(TAG, "save message:" + paramEMMessage.getMsgId());
    try
    {
      if (!this.allMessages.containsKey(paramEMMessage.getMsgId()))
      {
        addMessage(paramEMMessage);
        e.a().a(paramEMMessage);
      }
      addConversationToDB(paramEMMessage);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void saveMessage(EMMessage paramEMMessage, boolean paramBoolean)
  {
    EMLog.d(TAG, "save message:" + paramEMMessage.getMsgId());
    try
    {
      addMessage(paramEMMessage, paramBoolean);
      e.a().a(paramEMMessage);
      addConversationToDB(paramEMMessage);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public int getUnreadMsgsCount()
  {
    int i = 0;
    synchronized (this.conversations)
    {
      Collection localCollection = this.conversations.values();
      Iterator localIterator = localCollection.iterator();
      while (localIterator.hasNext())
        i += ((EMConversation)localIterator.next()).getUnreadMsgCount();
    }
    EMLog.d(TAG, "getunreadmsgcount return:" + i);
    return i;
  }

  public List<String> getConversationsUnread()
  {
    return e.a().g();
  }

  public synchronized void clear()
  {
    if (this.conversations != null)
      this.conversations.clear();
    if (this.tempConversations != null)
      this.tempConversations.clear();
    if (this.allMessages != null)
      this.allMessages.clear();
    this.allConversationsLoaded = false;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMConversationManager
 * JD-Core Version:    0.6.1
 */